CHS block

Short summary

Name

CHS

→POU type

→function

Category

IEC-block, NumericEnh, block with internal error diagnostic

Conform to →IEC-standard

(plus) not defined in IEC-standard

Graphical interface

Available since

version 1.64.0 (for Neuron Power Engineer)

Functionality

The block returns the value that is connected to the input IN with a changed sign. If the value 0 is connected, the block returns 0 again.

Behavior for unsigned integer data types (= ANY_UNSIGNED – see appropriate elementary data types for glossary item →generic data type):

  • The block returns an unsigned value. This value is formed as follows: The value connected to IN (if it is > 0) is subtracted from the upper limit of the appropriate data type, then the value 1 is added: upper limit – IN + 1
    See under "Example for usage within ST-editor".
    As background information: The value connected to IN is interpreted as bit pattern with sign. This signed value is converted (and returned as unsigned value again).

  • Furthermore, the output ENO is set to value FALSE.

Behavior for other data types:

  • Applies to ANY_SIGNED: If the lower limit of the appropriate data type is connected to IN, the block returns this value. Reason: The value with the changed sign is not allowed for the lower limit of the data type.
    See under "Example for usage within ST-editor".

  • Applies to ANY_SIGNED and ANY_REAL: The output ENO is always set to value TRUE.

Inputs, return value

 

Identifier

→Data type

Description

Inputs:

IN

REALLREALUSINTUINTUDINTULINTSINTINTDINT or LINT
(corresponds to →generic data type ANY_NUM)

value

Return value:

REAL, LREAL, USINT, UINT, UDINT, ULINT, SINT, INT, DINT or LINT
(corresponds to →generic data type ANY_NUM)

 

Input EN and output ENO are available when →calling the block. See "Execution control: EN, ENO" for information on input EN and output ENO.

See:

Internal error diagnostic for block

The block checks the data type of the connected value before the block is executed.
In case of an unsigned integer data type (= ANY_UNSIGNED), the output ENO of the block is set to value FALSE (or an equivalent).

Example for usage within ST-editor

The calculated values are evaluated by using the Assert block.

FUNCTION_BLOCK ExampleChs
  VAR
    resultSint1, resultSint2, resultSint3, resultSint4 : SINT;
    resultUsint1, resultUsint2, resultUsint3 : USINT;
    resultUint  : UINT;
    CheckENOSint1, CheckENOSint2, CheckENOSint3, CheckENOSint4, CheckENOUsint1, CheckENOUsint2, CheckENOUsint3, CheckENOUint : BOOL;
  END_VAR
  resultSint1 := CHS(IN := SINT#-5, ENO => CheckENOSint1);
  Assert(resultSint1 = 5);
  Assert(CheckENOSint1 = TRUE);
  resultSint2 := CHS(IN := SINT#0, ENO => CheckENOSint2);
  Assert(resultSint2 = 0);
  Assert(CheckENOSint2 = TRUE);
  resultSint3 := CHS(IN := SINT#5, ENO => CheckENOSint3);
  Assert(resultSint3 = -5);
  Assert(CheckENOSint3 = TRUE);
  resultSint4 := CHS(IN := SINT#-128, ENO => CheckENOSint4);  (* '-128' is the lower limit for SINT. *)
  Assert(resultSint4 = -128);                                 (* The upper limit for SINT is: 127. Therefore, value '128' is not possible. *)
  Assert(CheckENOSint4 = TRUE);
  resultUsint1 := CHS(IN := USINT#3, ENO => CheckENOUsint1);
  Assert(resultUsint1 = 253);          (* Upper limit for USINT = 255. Therefore, the block calculates: 255 - 3 + 1 = 253 *)
  Assert(CheckENOUsint1 = FALSE);
  resultUsint2 := CHS(IN := USINT#155, ENO => CheckENOUsint2);
  Assert(resultUsint2 = 101);          (* Upper limit for USINT = 255. Therefore, the block calculates: 255 - 155 + 1 = 101 *)
  Assert(CheckENOUsint2 = FALSE);
  resultUsint3 := CHS(IN := USINT#0, ENO => CheckENOUsint3);
  Assert(resultUsint3 = 0);            (* Upper limit for USINT = 255. The calculation '255 - 0 + 1' would result in the value '256' that is out of range. *)
  Assert(CheckENOUsint3= FALSE);
  resultUint := CHS(IN := UINT#3, ENO => CheckENOUint);
  Assert(resultUint = 65533);          (* Upper limit for UINT = 65535. Therefore, the block calculates: 65535 - 3 + 1 = 253 *)
  Assert(CheckENOUint = FALSE);
END_FUNCTION_BLOCK

When creating your application within the ST-editor, enter a call of a block by typing the text as requested by the syntax or use Content Assist.